import { isFunction } from "./utils"
import * as echarts from 'echarts'
import axios from "axios"

export class ChartUtils {
    
    constructor(dom, init) {
        this.option = {}
        this.dom = dom
        if (init && isFunction(init)) {
            this.chart = init()
        }
        else if (init) {
            this.chart = init
        }
        else {
            this.chart = null
        }
    }

    init() {

        this.chart = echarts.init(this.dom)
    }

    async registerMap(name, code) {
        let geojson = await axios.get(`data/hubei.json`)

        // console.log(geojson)

        echarts.registerMap(name, geojson.data)
    }

    addto(dom) {
        if (dom) {
            dom.appendChild(this.dom)
        }
    }

    setOption(option) {
        this.option = option
        // console.log(this.chart)
        this.chart.setOption(option)
    }

    onResize(func) {
        if (!this.chart) {
            return;
        }
        if (func) {
            func(this.chart, this.option)
        }
        else {

            this.chart.setOption(this.option)
            // console.log(this.dom.offsetWidth)
            this.chart.resize()
        }
    }
}